import request from '@/config/axios'

const baseURL = '/system/cloud-edit-manager'

export const getEditingProjectMaterials = (projectId: string) => {
  if (!projectId) throw new Error('请输入 projectId')
  return request.get({
    url: `${baseURL}/get-editing-project-materials`,
    params: { projectId }
  })
}

export const createEditingProject = (data: {
  title: string
  description?: string
  timeline?: string
  coverUrl?: string
  templateId?: string
  clipsParam?: string
  templateType?: string
  materialMaps?: string
  businessConfig?: string
  projectType?: string
}) => {
  if (!data.title) throw new Error('请输入 title')
  return request.post({
    url: `${baseURL}/create-editing-project`,
    data
  })
}

export const addEditingProjectMaterials = (data: { projectId: string; materialMaps: string }) => {
  if (!data.projectId) throw new Error('请输入 projectId')
  if (!data.materialMaps) throw new Error('请输入 materialMaps')
  return request.post({
    url: `${baseURL}/add-editing-project-materials`,
    data
  })
}

export const deleteEditingProjectMaterials = (data: {
  projectId: string
  materialIds: string[]
  materialType: 'Video' | 'Image' | 'Audio' | 'Subtitle' | 'Text'
}) => {
  return request.post({
    url: `${baseURL}/delete-editing-project-materials`,
    data: {
      projectId: data.projectId,
      materialIds: data.materialIds.join(','),
      materialType: data.materialType
    }
  })
}

export const getEditingProject = (data: { projectId: string; requestSource?: string }) => {
  return request.post({
    url: `${baseURL}/get-editing-project`,
    data
  })
}

export const updateEditingProject = (data: {
  projectId: string
  title?: string
  description?: string
  timeline?: string
  coverUrl?: string
  templateId?: string
  clipsParam?: string
  businessStatus?: 'Reserving' | 'ReservationCanceled'
}) => {
  return request.post({
    url: `${baseURL}/update-editing-project`,
    data
  })
}

export const submitMediaProducingJob = (data: {
  outputMediaConfig: string
  projectId?: string
  timeline?: string
  templateId?: string
  clipsParam?: string
  projectMetadata?: string
  outputMediaTarget?: 'oss-object' | 'vod-media'
  userData?: string
  clientToken?: string
  source?: 'OpenAPI' | 'AliyunConsole' | 'WebSDK'
  editingProduceConfig?: string
  mediaMetadata?: string
}) => {
  return request.post({
    url: `${baseURL}/submit-media-producing-job`,
    data
  })
}

export type EditingProject = {
  projectId: string
  title: string
  timeline: string
  description: string
  coverURL: string
  createTime: string
  modifiedTime: string
  duration: number
  status: 'Draft' | 'Producing' | 'Produced' | 'ProduceFailed'
  errorCode: string
  errorMessage: string
  createSource: 'OpenAPI' | 'AliyunConsole' | 'WebSDK'
  modifiedSource: string
  templateType: 'Timeline' | 'VETemplate'
  projectType: 'EditingProject' | 'LiveEditingProject'
}

export type SearchEditingProjectRequest = {
  startTime?: string // yyyy-MM-ddTHH:mm:ssZ
  endTime?: string
  status?: EditingProject['status']
  sortBy?: 'CreationTime:Desc' | 'CreationTime:Asc'
  pageNo?: number
  pageSize?: number
  createSource?: EditingProject['createSource']
  templateType?: EditingProject['templateType']
  projectType?: EditingProject['projectType']
}

export type SearchEditingProjectResponse = {
  requestId: string
  projectList: EditingProject[]
  maxResults: number
  totalCount: number
  nextToken: string
}

export const searchEditingProject = (
  data: SearchEditingProjectRequest
): SearchEditingProjectResponse => {
  return request.post({
    url: `${baseURL}/search-editing-project`,
    data
  }) as any
}

export const deleteEditingProject = (data: { projectId: string }) => {
  return request.get({
    url: `${baseURL}/delete-editing-projects?projectId=${data.projectId}`
  })
}
